//
//  YMFPlaceViewController.swift
//  FlyingPigeon
//
//  Created by yang on 2019/5/30.
//  Copyright © 2019年 a. All rights reserved.
//

import Foundation
import UIKit

class YMFPlaceModel: NSObject {
    var placeName = ""
    var imageName = ""
    var placeInfo = ""
    var time = ""
    
    init(dict: [String: String]) {
        super.init()
        
        placeName = dict["placeName"] ?? ""
        imageName = dict["imageName"] ?? ""
        placeInfo = dict["placeInfo"] ?? ""
        time = dict["time"] ?? ""
        
    }
    
    override init() {
        super.init()
    }
}


class YMFPlaceViewController: UIViewController {
    var dataArray = [YMFPlaceModel]()
    
    let tableView = UITableView()
//    let infoLabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        navigationItem.title = "站点选择"
        tableView.dataSource = self
        tableView.delegate = self
//        tableView.register(UINib(nibName: "YMFOrderCell", bundle: nil), forCellReuseIdentifier: "cell")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        tableView.backgroundColor = UIColor(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1)
        tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height - 64)
        view.addSubview(tableView)
        
        
//        infoLabel.text = "暂无订单信息"
//        infoLabel.textAlignment = .center
//        infoLabel.textColor = UIColor(red: 100 / 255.0, green: 100 / 255.0, blue: 100 / 255.0, alpha: 1)
//        infoLabel.isHidden = true
//        view.addSubview(infoLabel)
//        infoLabel.sizeToFit()
//        infoLabel.center = CGPoint(x: view.bounds.size.width * 0.5, y: view.bounds.size.height * 0.5)
        
       
        if let dataObj = YMFJsonFileManager.getObjectFormFile(fileName: "placeInfo") as? [[String:String]] {
            dataObj.forEach({ (item) in
                let model = YMFPlaceModel(dict: item)
                dataArray.append(model)
            })
        }
    }
    
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
    
//        dateArray = YMFOrderInfoManager.share().getData()
//        tableView.reloadData()
//        infoLabel.isHidden = dateArray.count != 0
//    }
    
    
}


extension YMFPlaceViewController:UITableViewDataSource, UITableViewDelegate  {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
        let model = dataArray[indexPath.row]
        cell?.imageView?.image = UIImage(named: model.imageName)
        cell?.imageView?.contentMode = .scaleToFill
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 128
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let vc = YMFPriceInfoViewController()
        vc.placeModel = dataArray[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
    }
}
